<?php
namespace app\channel\controller;
use think\Db;

class DataOrder extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $page = input('page');
            $limit = input('limit');
            $date = explodeTime(input('date'));
            $myUserId = input('my_user_id');
            $roleId = input('role_id');
            $myOrderId = input('my_order_id');
            $gameId = input('game_id');
            $channelId = input('channel_id');
            $gameResult = input('game_result');
            $payResult = input('pay_result');
            $plat = input('plat');

            $where = "order_time between '{$date['start_time']}' AND '{$date['end_time']}' && debug=0";

            if($plat == 'cps'){
                if( session('admin.cps_id') ){
                    $allChannelId = model('manage/Channel')->allChannelId( session('admin.cps_id') );
                    $where .= " && channel_id=1000 && reg_cps_id in ({$allChannelId})";
                }else{
                    $this->layTable(0, '', [], 0);
                }
            }
            if($plat == 'sdk'){
                if( session('admin.sdk_id') ){
                    $where .= " && channel_id=".session('admin.sdk_id');
                }else{
                    $this->layTable(0, '', [], 0);
                }
            }

            !empty($myUserId) && $where .= " && (my_user_id='{$myUserId}' OR my_user_name like '%{$myUserId}%')";
            !empty($roleId) && $where .= " && (role_id='{$roleId}' OR role_name like '%{$roleId}%')";
            !empty($myOrderId) && $where .= " && my_order_id='{$myOrderId}'";
            !empty($gameId) && $where .= " && game_id='{$gameId}'";
            if(!empty($gameResult)){
                if($gameResult == 1){
                    $where .= " && game_result='{$gameResult}'";
                }else{
                    $where .= " && game_result='0'";
                }
            }
            if(!empty($payResult)){
                if($payResult == 1){
                    $where .= " && pay_result='{$payResult}'";
                }else{
                    $where .= " && pay_result='0'";
                }
            }
            if( !empty($channelId) ){
                if($channelId >= 5000){ //联运
                    $where .= " && channel_id='{$channelId}'";
                }else{ //CPS
                    $where .= " && channel_id=1000 && reg_cps_id={$channelId}";
                }
            }

            $start = ($page-1)*$limit;
            $length = $limit;

            $y1 = date('Y', $date['start_time']);
            $y2 = date('Y', $date['end_time']);

            if($y1 == $y2){ //同一年
                $count = Db::name('PayOrderY_'.$y1)->where($where)->count();

                $select = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->limit($start, $length)
                    ->order('order_time desc')
                    ->select();

                $sum = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->sum('total_price');
            }else{ //跨年
                $field = "order_time, channel_id, reg_cps_id, game_id, my_user_id, my_user_name, role_id, role_name, server_id, server_name, my_order_id, ch_order_id, pay_order_id, cp_order_id, total_price, product_name, pay_type, pay_time, pay_result, game_result, ratio, coin, debug, rebate_time";
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($where)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$where})", true)
                    ->buildSql();

                $count = Db::table($subQuery.' y')->count();

                $select = Db::table($subQuery.' y')
                    ->limit($start, $length)
                    ->order('order_time desc')
                    ->select();

                $sum = Db::table($subQuery.' y')
                    ->sum('total_price');
            }

            $gameIdName = model('manage/Game')->gameIdName();
            $channelIdName = model('manage/Channel')->channelIdName();

            $data = [];
            foreach ($select as $k=>$v){
                $data[] = [
                    'order_time' => date('Y-m-d H:i:s', $v['order_time']),
                    'reg_cps_id' => $v['channel_id']==1000 ? $v['reg_cps_id'] : $v['channel_id'],
                    'reg_cps_name' => $v['channel_id']==1000 ? $channelIdName[ $v['reg_cps_id'] ] : $channelIdName[ $v['channel_id'] ],
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'my_user_id' => $v['my_user_id'],
                    'my_user_name' => $v['my_user_name'],
                    'role_id' => $v['role_id'],
                    'role_name' => $v['role_name'],
                    'server_id' => $v['server_id'],
                    'server_name' => $v['server_name'],
                    'cp_order_id' => $v['cp_order_id'],
                    'ch_order_id' => $v['ch_order_id'],
                    'my_order_id' => $v['my_order_id'],
                    'pay_order_id' => $v['pay_order_id'],
                    'total_price' => '¥ ' . $v['total_price']/100,
                    'product_name' => $v['product_name'],
                    'pay_type' => substr($v['pay_type'], 0, 5)=='wxpay' ? '微信' : '支付宝',
                    'pay_time' => $v['pay_time'] ? date('Y-m-d H:i:s', $v['pay_time']) : '',
                    'pay_result' => $v['pay_result'] ? '到账' : '',
                    'game_result' => $v['game_result'] ? '到账' : '',
                    'ratio' => '1' . '：' . $v['ratio'],
                    'coin' => $v['coin'],
                    'debug' => $v['debug'] ? '测试' : '',
                    'rebate_time' => $v['rebate_time'] ? date('Y-m-d H:i:s', $v['rebate_time']) : '未返利',
                ];
            }

            $this->layTable(0, '支付订单', $data, $count, round($sum/100, 2));
        }else{
            $date = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $channelData = model('Base')->channelNotice();
            $gameData = model('Base')->gameNotice();

            if(session('admin.cps_id') && session('admin.sdk_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.cps_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.sdk_id')){
                $cpsSelected = '';
                $sdkSelected = 'selected';
            }
            return view('', ['date'=>$date, 'channel_data'=>$channelData, 'game_data'=>$gameData, 'cps_selected'=>$cpsSelected, 'sdk_selected'=>$sdkSelected]);
        }
    }
}
